//
//  AuditFlowUtil.swift
//  ANOA
//
//  Created by 邱仙凯 on 2025/1/22.
//  Copyright © 2025 zwb. All rights reserved.
//


func go2AuditForm(_ one: ToDoModel, controller: UIViewController, jueseType: JueseTypeEnum = .auditer) {
    let dic = one.dictionaryRepresentation()
    let appr = AuditProcessAppear()
    appr.jueseType(jueseType)
    if one.auditReturnType == "ERROR" {
        appr.jueseType(.bohui)
    } else if one.auditReturnType == "SUCCESS" {
        appr.jueseType(.completed)
    }
    
   
    if go2Immobilizationable(one) {
        immobilizationDeal(one: one, jueseType:jueseType, controller: controller)
    } else {
        continueDeal()
    }
    
    func continueDeal() {
        
       if one.isCustom == "1" {
           
           var moduleId = one.moduleId.unwrap
           if checkPlatform(type: .haiXin) {
               moduleId = one.moduleIdNew.unwrap
           }
           
           getTempMenu(id: moduleId) { _ in

               let onf = ["xx", "id_key=${vou_id}", "0", "form", one.appMenuId.unwrap]
               var audiC = AuditParamCombine()
               audiC.appr = appr
               audiC.model = one
               go2SpeMenuForm(oneXX: onf, one: dic, straightDic: [:], vc: controller,excuteType: .auditing, auditParamCombine: audiC)
           }
       } else {
    
           let vc = GeneralAuditForm()
           vc.appr = appr
           vc.type4web = (one.isCustom == "2")
           
           vc.model = one
           controller.navigationController?.pushViewController(vc, animated: true)
       }
    }
    
        
       
    
  
}
// 处理拼接底部的 最新审核信息 和审核按钮
// backClo  返回的result  具体的底部view
func dealFooterAuditFlow(model:ToDoModel,backClo:@escaping((TodoResult,[ChooseUsers])->())) {
    KSNetwork.shared.doRequest(API.auditFlow(.WflowTaskSet(taskId: model.taskId.unwrap))) { (data:BaseResult<TaskSetResult>) in
        var ss = [ChooseUsers]()
        if data.flag == TRUE {
            
            if let info = data.infoObjCombine {
                 ss = info.chooseUsers
            }
             
        } else {
            
        }
        getProcessInfo(model:model,chooseUsers: ss,backClo: backClo)
    } errorHandle: { err in
        getProcessInfo(model:model,chooseUsers: [],backClo: backClo)
    }
}
func getProcessInfo(model:ToDoModel,chooseUsers:[ChooseUsers],backClo:@escaping((TodoResult,[ChooseUsers])->())) {
    var api:API!
    if model.instanceId.unwrap != "" {
        api =  API.auditFlow(.progressTaskInfo(instId: model.instanceId.unwrap, nodeId: model.nodeId.unwrap))
    } else {
        api =   API.auditFlow(.progressTaskInfoByVoc(voc_cd: model.vocCd.unwrap, vou_id: model.vouId.unwrap))
    }
    
    KSNetwork.shared.doRequest(api) { (data:BaseResult<TodoResult>) in
       
        if data.flag == TRUE {
            if var info = data.infoObjCombine {
                if let newflow = info.newflow {
                    info.progress = newflow.progress
                }
                
                backClo(info,chooseUsers)
                
            }
            
            
        } else {
            KSToast(data.errorCombine.unwrap)
        }
    }
}
